using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace QtDllTestDotNET{ public partial class frmMain : Form { public frmMain() { InitializeComponent(); } void MSG(string msg) { lboxMSG.Items.Add(msg " 时间:" DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } [DllImport("HT110BServer.dll")] static extern bool InitServer(int port); [DllImport("HT110BServer.dll")] static extern void CloseServer(); [DllImport("HT110BServer.dll")] static extern void BuFang(string hostID, byte[] data); [DllImport("HT110BServer.dll")] static extern void CheFang(string hostID, byte[] data); [UnmanagedFunctionPointer(CallingConvention.Winapi)] delegate void CallBackHostConnect(string hostID); [DllImport("HT110BServer.dll")] static extern void HostConnect([MarshalAs(UnmanagedType.FunctionPtr)] CallBackHostConnect CallBack); CallBackHostConnect cOnEventHostConnect = null; void OnEventHostConnect(string hostID) { MSG("主机上线:" hostID); cboxHostID.Items.Add(hostID); cboxHostID.SelectedIndex = 0; } [UnmanagedFunctionPointer(CallingConvention.Winapi)] delegate void CallBackHostDisConnect(string hostID); [DllImport("HT110BServer.dll")] static extern void HostDisConnect([MarshalAs(UnmanagedType.FunctionPtr)] CallBackHostDisConnect CallBack); CallBackHostDisConnect cOnEventHostDisConnect = null; void OnEventHostDisConnect(string hostID) { MSG("主机下线:" hostID); cboxHostID.Items.Remove(hostID); } [UnmanagedFunctionPointer(CallingConvention.Winapi)] delegate void CallBackHostBuFang(string hostID); [DllImport("HT110BServer.dll")] static extern void HostBuFang([MarshalAs(UnmanagedType.FunctionPtr)] CallBackHostBuFang CallBack); CallBackHostBuFang cOnEventHostBuFang = null; void OnEventHostBuFang(string hostID) { MSG("主机布防:" hostID); } [UnmanagedFunctionPointer(CallingConvention.Winapi)] delegate void CallBackHostCheFang(string hostID); [DllImport("HT110BServer.dll")] static extern void HostCheFang([MarshalAs(UnmanagedType.FunctionPtr)] CallBackHostCheFang CallBack); CallBackHostCheFang cOnEventHostCheFang = null; void OnEventHostCheFang(string hostID) { MSG("主机撤防:" hostID); } [UnmanagedFunctionPointer(CallingConvention.Winapi)] delegate void CallBackHostAlarm(string hostID, string defenceID); [DllImport("HT110BServer.dll")] static extern void HostAlarm([MarshalAs(UnmanagedType.FunctionPtr)] CallBackHostAlarm CallBack); CallBackHostAlarm cOnEventHostAlarm = null; void OnEventHostAlarm(string hostID,string defenceID) { MSG("主机报警:" hostID " 防区号:" defenceID); } private void Form1_Load(object sender, EventArgs e) { cOnEventHostConnect = OnEventHostConnect; HostConnect(cOnEventHostConnect); cOnEventHostDisConnect = OnEventHostDisConnect; HostDisConnect(cOnEventHostDisConnect); cOnEventHostBuFang = OnEventHostBuFang; HostBuFang(cOnEventHostBuFang); cOnEventHostCheFang = OnEventHostCheFang; HostCheFang(cOnEventHostCheFang); cOnEventHostAlarm = OnEventHostAlarm; HostAlarm(cOnEventHostAlarm); } private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { CloseServer(); } private void btnListen_Click(object sender, EventArgs e) { MSG("服务初始化:" InitServer(Convert.ToInt32(txtPort.Text))); } byte[] HexStringToByteArray(string s) { s = s.Replace(" ", ""); byte[] buffer = new byte[s.Length / 2]; for (int i = 0; i < s.Length; i = 2) buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16); return buffer; } private void btnBuFang_Click(object sender, EventArgs e) { byte[] data = HexStringToByteArray(txtBuFangData.Text); BuFang(cboxHostID.Text, data); } private void btnCheFang_Click(object sender, EventArgs e) { byte[] data = HexStringToByteArray(txtCheFangData.Text); CheFang(cboxHostID.Text, data); } }}
评论